<?php
/**
 * User zgh
 * Email 1803897171@qq.com
 * Date 2022/1/14
 **/

namespace app\admin\behavior;


use app\common\model\AdminRoleModel;
use think\Model;

class AdminRoleHook
{

    public function run($params)
    {
        $admin_id = $params['admin_id'];
        $role_ids = $params['role_ids'];
        AdminRoleModel::where(["admin_id" => $admin_id])->delete();
        if (!empty($role_ids)) {
            if (is_string($role_ids)) {
                $role_ids = explode(",", $role_ids);
            }
            $role_arr = [];
            foreach ($role_ids as $role_id) {
                array_push($role_arr, ["admin_id" => $admin_id, "role_id" => $role_id]);
            }
            $role_model = new AdminRoleModel();
            $role_model->saveAll($role_arr);
        }
    }
}